import { createRouter, createWebHistory } from 'vue-router'
import { useUserStore } from '@/store/user'
import NProgress from 'nprogress'
import 'nprogress/nprogress.css'

NProgress.configure({ showSpinner: false })
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/login',
      name: 'login',
      meta: {
        title: '登录',
      },
      component: () => import('@/views/Login.vue'),
    },
    {
      path: '/',
      redirect: '/login',
    },
    {
      path: '/index',
      name: 'index',
      redirect: '/index/home',
      meta: {
        title: '酒店信息管理系统',
      },
      component: () => import('@/views/Index.vue'),
      children: [
        {
          path: 'home',
          name: 'home',
          meta: {
            title: '首页',
          },
          component: () => import('@/views/Home.vue'),
        },
        {
          path: 'mail',
          name: 'mail',
          meta: {
            title: '邮箱',
          },
          component: () => import('@/views/Mail.vue'),
        },
        {
          path: 'message',
          name: 'message',
          meta: {
            title: '消息',
          },
          component: () => import('@/views/Message.vue'),
        },
        {
          path: 'livein',
          name: 'livein',
          meta: {
            title: '入住管理',
          },
          component: () => import('@/views/custom/Livein.vue'),
        },
        {
          path: 'order',
          name: 'order',
          meta: {
            title: '订单管理',
          },
          component: () => import('@/views/custom/Order.vue'),
        },
        {
          path: 'room',
          name: 'room',
          meta: {
            title: '房间数量',
          },
          component: () => import('@/views/room/Room.vue'),
        },
        {
          path: 'roomtype',
          name: 'roomtype',
          meta: {
            title: '房间类型',
          },
          component: () => import('@/views/room/RoomType.vue'),
        },
        {
          path: 'menu',
          name: 'menu',
          meta: {
            title: '菜单管理',
          },
          component: () => import('@/views/system/Menu.vue'),
        },
        {
          path: 'dict',
          name: 'dict',
          meta: {
            title: '字典管理',
          },
          component: () => import('@/views/system/Dictionary.vue'),
        },
        {
          path: 'mine',
          name: 'mine',
          meta: {
            title: '个人中心',
          },
          component: () => import('@/views/user/Mine.vue'),
        },
        {
          path: 'setpwd',
          name: 'setpwd',
          meta: {
            title: '修改密码',
          },
          component: () => import('@/views/user/SetPwd.vue'),
        },
        {
          path: 'user',
          name: 'user',
          meta: {
            title: '用户管理',
          },
          component: () => import('@/views/user/User.vue'),
        },
        {
          path: 'role',
          name: 'role',
          meta: {
            title: '角色管理',
          },
          component: () => import('@/views/user/Role.vue'),
        },
      ],
    },
  ],
})

router.beforeEach((to, from, next) => {
  NProgress.start()
  const userStore = useUserStore()
  if (to.name !== 'login' && !userStore.isAuthenticated) {

    next({ name: 'login' })
  } else if (to.name == 'login' && userStore.isAuthenticated) {
    next('/index')
  } else {
    next()
  }
})

router.afterEach((to) => {
  document.title = to?.meta?.title as string
  NProgress.done()
  // if (to.meta.title) {
  //   document.title = to.meta.title as string
  // }
})

export default router
